package com.chen.blog.common.exception;

import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.constant.MessageConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description 全局异常处理
 * @Author zgyh2
 * @Date 2022/1/23
 * @Version 1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(UsernameNotFoundException.class)
    public CommonResult usernameNotFoundException(UsernameNotFoundException e) {
        log.info(e.getMessage());
        return CommonResult.error(MessageConstant.ADMIN_LOGIN_FAIL);
    }

    @ExceptionHandler(NullPointerException.class)
    public CommonResult NullPointerException(NullPointerException e) {
        log.info(e.getMessage());
        return CommonResult.error(MessageConstant.DATA_NULL);
    }

    @ExceptionHandler(BadSqlGrammarException.class)
    public CommonResult BadSqlGrammarException(BadSqlGrammarException e) {
        log.info(e.getMessage());
        return CommonResult.success(null);
    }

    @ExceptionHandler(Exception.class)
    public CommonResult myException(Exception e) {
        log.info(e.getMessage());
        return CommonResult.error(MessageConstant.MYSQL_DATA_FAIL);
    }
}